package org.javabb.bbcode;

/*
 * Copyright 2004 JavaFree.org
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.Serializable;

/**
 * $Id: BBCodeProcessor.java,v 1.1 2005/02/07 03:16:07 ronaldtm Exp $
 *
 * @author Ronald Tetsuo Miura
 */
public class BBCodeProcessor implements Serializable {
    /**
     *
     */
    private static final long serialVersionUID = 7098989776532946445L;


    private static final Tag[] REGEX_TAGS = new Tag[]{
            new CodeTag(),
            new QuoteTag(),
            new ListTag(),
            new SimpleRegexTag("", "(\r\n|\n\r|\n|\r)", "<br>"),
            new SimpleRegexTag("color",
                    "\\[color=['\"]?(.*?[^'\"])['\"]?\\](.*?)\\[/color\\]",
                    "<span style=\"color:$1;\">$2</span>"),
            new SimpleRegexTag("size",
                    "\\[size=['\"]?([0-9]|[1-2][0-9])['\"]?\\](.*?)\\[/size\\]",
                    "<span style=\"font-size:$1px;\">$2</span>"),
            new SimpleRegexTag("b", "\\[b\\](.*?)\\[/b\\]", "<b>$1</b>"),
            new SimpleRegexTag("u", "\\[u\\](.*?)\\[/u\\]", "<u>$1</u>"),
            new SimpleRegexTag("i", "\\[i\\](.*?)\\[/i\\]", "<i>$1</i>"),
            new SimpleRegexTag("img", "\\[img\\](.*?)\\[/img\\]", "<img src='$1' border='0' alt=''>"),
            new SimpleRegexTag("url", "\\[url\\](.*?)\\[/url\\]", "<a href='$1' target='_blank'>$1</a>"),
            new SimpleRegexTag("url",
                    "\\[url=['\"]?(.*?[^'\"])['\"]?\\](.*?)\\[/url\\]",
                    "<a href=\"$1\" target=\"_blank\">$2</a>"),
            new SimpleRegexTag("email", "\\[email\\](.*?)\\[/email\\]", "<a href='mailto:$1'>$1</a>")};

    /** */
    private boolean acceptHTML = false;

    /** */
    private boolean acceptBBCode = true;

    /**
     * @return acceptBBCode
     */
    public boolean getAcceptBBCode() {
        return acceptBBCode;
    }

    /**
     * @param acceptBBCode the new acceptBBCode value
     */
    public void setAcceptBBCode(boolean acceptBBCode) {
        this.acceptBBCode = acceptBBCode;
    }

    /**
     * @return htmlAccepted
     */
    public boolean getAcceptHTML() {
        return acceptHTML;
    }

    /**
     * @param acceptHTML the new acceptHTML value
     */
    public void setAcceptHTML(boolean acceptHTML) {
        this.acceptHTML = acceptHTML;
    }

    /**
     * @param texto the text to
     * @return TODO unuseful parameters.
     */
    public String preparePostText(String texto) {
        if (!getAcceptHTML()) {
            texto = replaceHTML(texto);
        }
        if (getAcceptBBCode()) {
            texto = process(texto);
        }
        return texto;
    }

    /**
     * @param string string for process
     * @return HTML-formated message
     */
    private String process(final String string) {
        String temp = string;
        for (Tag tag : REGEX_TAGS) {
            temp = tag.processTag(temp);
        }
        return temp;
    }


    /**
     * @param inputText input string for tranformation
     * @return text transformed string
     */
    public static String replaceHTML(String inputText) {
        return inputText.replaceAll("<", "&lt;").replaceAll(">", "&gt;");
    }
}
